# -*- encoding=utf8 -*-


from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest.core.helper import G

# if not cli_setup():
auto_setup(__file__, logdir=False, devices=["Android:///",])


class baseclass(object):
    package = None
    width = None
    heigth = None

    def __init__(self):

        if self.package:
            stop_app(package=self.package)
            start_app(package=self.package)
            sleep(2.0)
            self.skip_security()
        for i in shell('wm size').splitlines():
            width,heigth=i.split()[2].split('x')
            self.width,self.heigth=int(width),int(heigth)
            if 'Override size' in i:
                break

    def loginfo(self, str):
        G.LOGGING.info(f"\033[1;24m {str} \033[0m")

    def return_target_page(self, tmp=Template(r"tpl1623080609408.png", record_pos=(0.397, 0.911), resolution=(1080, 2400))):
        for i in range(5):
            # tmp=Template(r"tpl1623080609408.png", record_pos=(0.397, 0.911), resolution=(1080, 2400))
            if not exists(tmp):
                self.keyevent("KEYCODE_BACK")

    def check_touch(self,Template):
        if exists(Template):
            self.touch(Template)
            self.loginfo(f'如存在便点击目标{str(Template)}')
            return True
        self.loginfo(f'无目标控件{str(Template)}')
        return False

    def touch(self,v, times=1, **kwargs):
        self.loginfo(f'点击控件{v}')
        touch(v,times,**kwargs)

    def skip_security(self):
        self.loginfo("安全弹框")
        self.check_touch(Template(r"tpl1623058735694.png", record_pos=(-0.217, 0.83), resolution=(1080, 2400)))

    def keyevent(self,keyname, **kwargs):
        self.loginfo(f'按{keyname}键')
        keyevent(keyname, **kwargs)

    def __del__(self):
        self.loginfo("工作完成")
    #     stop_app(package="com.jingdong.app.mall")

    def swipe_up(self):
        self.loginfo('上滑')
        shell(f'input swipe {self.width/2} {self.heigth*0.8} {self.width/2} {self.heigth*0.2} 500')

    def swipe_down(self):
        self.loginfo('下滑')
        shell(f'input swipe {self.width/2} {self.heigth*0.2} {self.width/2} {self.heigth*0.8} 500')

    def swipe_left(self):
        self.loginfo('左滑')
        shell(f'input swipe {self.width*0.8} {self.heigth/2} {self.width*0.2} {self.heigth/2} 500')

    def swipe_right(self):
        self.loginfo('右滑')
        shell(f'input swipe {self.width*0.2} {self.heigth/2} {self.width*0.8} {self.heigth/2} 500')

class jd(baseclass):
    package = "com.jingdong.app.mall"

    def __init__(self):
        super().__init__()
        self.touch(Template(r"tpl1623058766207.png", record_pos=(0.395, 0.909), resolution=(1080, 2400)))

    # def __del__(self):
    #     print("退出京东")
    #     stop_app(package="com.jingdong.app.mall")

    def jdnc(self):
        self.return_target_page()
        self.loginfo("进入京东农场")
        self.touch(Template(r"tpl1623058938827.png", record_pos=(-0.352, 0.497), resolution=(1080, 2400)))
        sleep(4.0)
        for i in range(3):
            self.check_touch(Template(r"tpl1623059778551.png", record_pos=(-0.197, 0.325), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623058997398.png", record_pos=(-0.376, 0.484), resolution=(1080, 2400)))
        self.loginfo("点击连续签到")
        self.check_touch(Template(r"tpl1623059018274.png", record_pos=(0.217, -0.292), resolution=(1080, 2400)))
        self.loginfo("连续签到")
        if self.check_touch(Template(r"tpl1623059778551.png", record_pos=(-0.197, 0.325), resolution=(1080, 2400))):
            self.touch(Template(r"tpl1623060483462.png", record_pos=(-0.202, 0.294), resolution=(1080, 2400)))
        self.loginfo("3个关注水滴")
        for i in range(3):
            if self.check_touch(Template(r"tpl1623060946491.png", record_pos=(-0.259, 0.685), resolution=(1080, 2400))):
                sleep(5.0)
                if exists(Template(r"tpl1623067077237.png", record_pos=(-0.003, 0.657), resolution=(1080, 2400))):
                    self.keyevent("KEYCODE_BACK")
                self.keyevent("KEYCODE_BACK")
                touch(Template(r"tpl1623061634351.png", record_pos=(-0.26, 0.384), resolution=(1080, 2400)))
            else:
                break
        if not exists(Template(r"tpl1623067289527.png", record_pos=(-0.221, 0.584), resolution=(1080, 2400))):
            self.keyevent("KEYCODE_BACK")
        self.loginfo("领水滴")
        self.touch(Template(r"tpl1623067289527.png", record_pos=(-0.221, 0.584), resolution=(1080, 2400)))
        sleep(2.0)
        self.loginfo("点击去逛逛")
        for i in range(9):
            if self.check_touch(Template(r"tpl1623067326326.png", threshold=0.9000000000000001, record_pos=(0.325, 0.511), resolution=(1080, 2400))):
                sleep(7.0)
                self.skip_security()
                if not exists(Template(r"tpl1623068188701.png", record_pos=(0.002, -0.168), resolution=(1080, 2400))):
                    self.keyevent("KEYCODE_BACK")
                self.check_touch(Template(r"tpl1623068415954.png", record_pos=(0.331, 0.508), resolution=(1080, 2400)))
            else:
                break
        self.touch(Template(r"tpl1623073598316.png", record_pos=(0.456, -0.146), resolution=(1080, 2400)))
        self.loginfo("点击每日累计浇水10次")
        # for i in range(6):
        #     print('查看是否浇满10次')
        #     # if not exists(Template(r"tpl1623206327526.png", record_pos=(-0.204, 0.464), resolution=(1080, 2400))):
        #     print('浇水')
        #     touch(Template(r"tpl1623073687811.png", record_pos=(0.313, 0.477), resolution=(1080, 2400)))
        #     sleep(1.0)
        self.touch(Template(r"tpl1623067289527.png", record_pos=(-0.221, 0.584), resolution=(1080, 2400)))
        sleep(2.0)
        self.loginfo("点击去逛逛")
        for i in range(2):
            self.loginfo("点击领取")
            self.check_touch(Template(r"tpl1623207771786.png", record_pos=(0.331, 0.06), resolution=(1080, 2400)))
            sleep(1.0)
        self.check_touch(Template(r"tpl1623207782542.png", record_pos=(0.33, 0.285), resolution=(1080, 2400)))

    def jdqd(self):
        self.loginfo('京东签到')
        self.return_target_page()
        self.touch(Template(r"tpl1623081086232.png", record_pos=(-0.176, 0.501), resolution=(1080, 2400)))
        self.check_touch(Template(r"tpl1623081112506.png", threshold=0.9, record_pos=(-0.033, -0.806), resolution=(1080, 2400)))
        
    def jdmc(self):
        self.loginfo('京东萌宠')
        self.return_target_page()
        self.touch(Template(r"tpl1623081086232.png", record_pos=(-0.176, 0.501), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623081112506.png", threshold=0.9, record_pos=(-0.033, -0.806), resolution=(1080, 2400)))
        
    def jdcww(self):
        # 京东宠旺旺
        self.return_target_page()
        if self.check_touch(Template(r"tpl1623209840546.png", threshold=0.9, record_pos=(0.343, 0.668), resolution=(1080, 2400))):
            self.loginfo('进入赚狗粮')
            self.touch(Template(r"tpl1623210134635.png", record_pos=(-0.39, 0.848), resolution=(1080, 2400)))
            self.loginfo('点击每日签到')
            if self.check_touch(Template(r"tpl1623210156684.png", threshold=0.93, record_pos=(0.34, 0.096), resolution=(1080, 2400))):
                touch(Template(r"tpl1623218096889.png", record_pos=(0.009, 0.323), resolution=(1080, 2400)))
                touch(Template(r"tpl1623218285664.png", record_pos=(-0.004, 0.277), resolution=(1080, 2400)))
            self.loginfo('首次投喂')
            if self.check_touch(Template(r"tpl1623377728063.png", record_pos=(-0.025, 0.088), resolution=(1080, 2400))):
                self.touch(Template(r"tpl1623210134635.png", record_pos=(-0.39, 0.848), resolution=(1080, 2400)))
                self.check_touch(Template(r"tpl1623218885154.png", threshold=0.9, record_pos=(0.346, 0.556), resolution=(1080, 2400)))

            self.check_touch(Template(r"tpl1623210216436.png", threshold=0.9, record_pos=(0.343, 0.523), resolution=(1080, 2400)))
            for i in range(5):
                self.loginfo('点击已存在的已领取')
                if not self.check_touch(Template(r"tpl1623218885154.png", threshold=0.9, record_pos=(0.346, 0.556), resolution=(1080, 2400))):
                    break
            for i in range(5):
                self.loginfo(f'点击去浏览{i}')
                if self.check_touch(Template(r"tpl1623210265044.png", threshold=0.9, record_pos=(0.344, 0.809), resolution=(1080, 2400))):
                    sleep(9)
                    self.return_target_page(Template(r"tpl1623223105837.png", record_pos=(0.003, -0.231), resolution=(1080, 2400)))
                    self.touch(Template(r"tpl1623218885154.png", record_pos=(0.346, 0.556), resolution=(1080, 2400)))
                    sleep(1)
                else:
                    break

                    
class mt(baseclass):
    package = "com.sankuai.meituan"

    def __init__(self):
        super().__init__()
        for i in range(3):
            if self.check_touch(Template(r"tpl1623229942131.png", threshold=0.9, record_pos=(-0.184, -0.232), resolution=(1080, 2400))):
                break
            stop_app(package=self.package)
            start_app(package=self.package)
        sleep(3)
        self.check_touch(Template(r"tpl1623229991634.png", record_pos=(0.412, -0.547), resolution=(1080, 2400)))

    def lsd(self,time=1):
        self.loginfo('领水滴')
        self.touch(Template(r"tpl1623232057025.png", record_pos=(-0.393, 0.686), resolution=(1080, 2400)))
        for i in range(time):
            if self.check_touch(Template(r"tpl1623230183370.png", record_pos=(0.32, -0.299), resolution=(1080, 2400))):
                self.touch(Template(r"tpl1623231785583.png", record_pos=(0.019, 0.459), resolution=(1080, 2400)))
            else:
                break

    def mtsg(self):
        self.loginfo('超大水滴')
        self.touch(Template(r"tpl1623246387793.png", target_pos=2, record_pos=(-0.395, -0.668), resolution=(1080, 2400)))
        if self.check_touch(Template(r"tpl1623230055161.png", threshold=0.9, record_pos=(0.289, -0.541), resolution=(1080, 2400))):
            self.touch(Template(r"tpl1623231785583.png", record_pos=(0.019, 0.459), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623231977925.png", record_pos=(0.399, -0.73), resolution=(1080, 2400)))
        self.lsd(3)

        for i in range(3):
            self.loginfo('浏览15秒')
            if self.check_touch(Template(r"tpl1623232198209.png", threshold=0.9, target_pos=6, record_pos=(-0.017, 0.306), resolution=(1080, 2400))):
                for i in range(4):
                    self.swipe_down()
                    sleep(4)
                self.keyevent("KEYCODE_BACK")
                self.lsd()
            else:
                break


        self.loginfo('浏览5秒')
        if self.check_touch(Template(r"tpl1623244854864.png", threshold=0.9, target_pos=6, record_pos=(-0.026, 0.121), resolution=(1080, 2400))):
            for i in range(8):
                sleep(5)
                self.keyevent("KEYCODE_BACK")
                if not self.check_touch(Template(r"tpl1623244942288.png", record_pos=(0.008, 0.246), resolution=(1080, 2400))):
                    break

        self.loginfo('浏览5秒2')
        self.swipe_up()
        for i in range(3):
            if self.check_touch(Template(r"tpl1623245866105.png", threshold=0.9, target_pos=6, record_pos=(-0.021, 0.748), resolution=(1080, 2400))):
                sleep(5)
                self.keyevent("KEYCODE_BACK")
                self.lsd()

    def gyyl(self):
        self.loginfo('天天抽奖')
        self.touch(Template(r"tpl1623247074458.png", threshold=0.9, record_pos=(-0.182, 0.67), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623247329613.png", threshold=0.9, target_pos=6, record_pos=(-0.017, 0.065), resolution=(1080, 2400)))
        if self.check_touch(Template(r"tpl1623247205662.png", threshold=0.9, record_pos=(-0.006, 0.518), resolution=(1080, 2400))):
            self.touch(Template(r"tpl1623247819550.png", threshold=0.9, record_pos=(0.001, 0.345), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623247232137.png", threshold=0.9, record_pos=(0.377, -0.612), resolution=(1080, 2400)))

        self.loginfo('免费赢水滴')
        self.touch(Template(r"tpl1623247074458.png", threshold=0.9, record_pos=(-0.182, 0.67), resolution=(1080, 2400)))
        self.touch(Template(r"tpl1623247171872.png", threshold=0.9, target_pos=6, record_pos=(-0.019, 0.315), resolution=(1080, 2400)))
        sleep(2)
        self.check_touch(Template(r"tpl1623247643618.png", threshold=0.9, record_pos=(-0.003, 0.596), resolution=(1080, 2400)))

        

if __name__ == '__main__':
    jd().jdcww()





# generate html report
# from airtest.report.report import simple_report
# simple_report(__file__, logpath=True)


























































